package com.ruoyi.exhibition.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.file.domain.Files;
import com.ruoyi.file.domain.vo.FilesVo;
import com.ruoyi.file.mapper.FilesMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.exhibition.domain.bo.ExhibitionBo;
import com.ruoyi.exhibition.domain.vo.ExhibitionVo;
import com.ruoyi.exhibition.domain.Exhibition;
import com.ruoyi.exhibition.mapper.ExhibitionMapper;
import com.ruoyi.exhibition.service.IExhibitionService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 展项列Service业务层处理
 *
 * @author ztx
 * @date 2023-03-11
 */
@RequiredArgsConstructor
@Service
public class ExhibitionServiceImpl implements IExhibitionService {

    private final ExhibitionMapper baseMapper;

    private final FilesMapper filesMapper;
    /**
     * 查询展项列
     */
    @Override
    public ExhibitionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询展项列列表
     */
    @Override
    public TableDataInfo<ExhibitionVo> queryPageList(ExhibitionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Exhibition> lqw = buildQueryWrapper(bo);
        Page<ExhibitionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询展项列列表
     */
    @Override
    public List<ExhibitionVo> queryList(ExhibitionBo bo) {
        LambdaQueryWrapper<Exhibition> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Exhibition> buildQueryWrapper(ExhibitionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Exhibition> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getZxName()), Exhibition::getZxName, bo.getZxName());
        lqw.eq(StringUtils.isNotBlank(bo.getZxFloor()), Exhibition::getZxFloor, bo.getZxFloor());
        lqw.eq(StringUtils.isNotBlank(bo.getZxArea()), Exhibition::getZxArea, bo.getZxArea());
        return lqw;
    }

    /**
     * 新增展项列
     */
    @Override
    public Boolean insertByBo(ExhibitionBo bo) {
        Exhibition add = BeanUtil.toBean(bo, Exhibition.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改展项列
     */
    @Override
    public Boolean updateByBo(ExhibitionBo bo) {
        Exhibition update = BeanUtil.toBean(bo, Exhibition.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Exhibition entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除展项列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        List<ExhibitionVo> exhibitionVos = baseMapper.selectVoBatchIds(ids);
        ArrayList<String> paths = new ArrayList<>();
        for (ExhibitionVo exhibitionVo : exhibitionVos) {
            String smallImg = exhibitionVo.getSmallImg();
            String bigImg = exhibitionVo.getBigImg();
            String audioPath = exhibitionVo.getAudioPath();
            paths.add(smallImg);
            paths.add(bigImg);
            paths.add(audioPath);
        }
        LambdaQueryWrapper<Files> qw = Wrappers.lambdaQuery();
        qw.in(Files::getMapperPath,paths);
        List<FilesVo> filesVos = filesMapper.selectVoList(qw);
        for (FilesVo filesVo : filesVos) {
            FileUtil.del(new File(filesVo.getRealPath()));
        }
        filesMapper.delete(qw);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
